﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Shapes;
using System.Drawing;
using Silvermoon.OpenGL;
using Silvermoon.Controls.Base;

namespace Silvermoon.Controls.Beta
{
    public interface IGroupHeader : IDisposable
    {
        string Title { get; set; }
    }

    /// <summary>
    /// Special Item to provide a header in a <see cref="T:ListBoxBase"/>. A GroupHeader has a special function in a ListBox:
    /// When available, the ListBox always shows the last GroupHeader as top item.
    /// </summary>
    public class GroupHeader : ChromeControl, IGroupHeader
    {
        #region Fields

        private string title = String.Empty;
        private StringShape stringShape;
        private Color color;

        #endregion
        #region ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="SectionHeader"/> class.
        /// </summary>
        public GroupHeader()
            : base()
        {
            stringShape = new StringShape
            {
                Color = Color.White,
                CharSpace = 2,
                Alignment = Alignment.Near,
                LineAlignment = Alignment.Center,
                LineMode = LineMode.EndEllipsis
            };

            Shapes.AddLast(stringShape);

            TextColor = SysColors.ItemHeaderTextColor;
            color = SysColors.ItemHeaderColor;

        }


        #endregion
        #region Props

        public Alignment Alignment
        {
            get { return stringShape.Alignment; }
            set
            {
                if (stringShape.Alignment != value)
                {
                    stringShape.Alignment = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        public FontSprite Font
        {
            get { return stringShape.Font; }
            set
            {
                if (stringShape.Font != value)
                {
                    stringShape.Font = value;
                    Invalidate(InvalidationFlag.LayoutAppearance);
                }
            }
        }


        public string Title
        {
            get { return title; }
            set
            {
                if (title != value)
                {
                    title = value;
                    Invalidate(InvalidationFlag.Text);
                }
            }
        }

        #endregion
        #region methods

        protected override Shape CreateDefaultBackground()
        {
            return new BackgroundShape { Style = RenderStyle.Gradiented, Opacity = 200 };
        }


        private void SetTextBounds()
        {
            stringShape.Bounds = Control.ScaledRect(10, 2, Width - 20, Height - 4);
        }

        protected override void OnLayout()
        {
            stringShape.Text = title;
            if (CheckFlag(InvalidationFlag.TextColor))
            {
                stringShape.Color = TextColor;
            }

            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Margin | InvalidationFlag.Layout))
            {
                SetTextBounds();
            }
            base.OnLayout();
        }

        #endregion
    }
}
